package Red_Black_Tree;


/*
 * This is an implementation of Red-Black tree (RBTree) node.
 */


public abstract class Node {

	/*
	 * In this abstract class, there're only basic elements that needed for a Red-Black tree.
	 * More data fields could be added when an instance of this class is created.
	 */
	protected boolean isRed;
	protected boolean isLeaf;
	protected Node parent, lChild, rChild;
	
	public Node(){
		isRed = true;
		isLeaf = false;
		parent = null;
		lChild = null;
		rChild = null;
	}
	
	public void setParent(Node p){
		parent = p;
	}
	public void setLChild(Node l){
		lChild = l;
	}
	public void setRChild(Node r){
		rChild = r;
	}
	public void setBothChildrenLeaf(Node n){
		lChild = n;
		rChild = n;
	}
	public void setRed(){
		isRed = true;
	}
	public void setBlack(){
		isRed = false;
	}
	public void setLeaf(){
		isLeaf = true;
	}
	public void setNotLeaf(){
		isLeaf = false;
	}
	public boolean isRed(){
		return isRed;
	}
	public boolean isLeaf(){
		return isLeaf;
	}
	
	public Node getParent(){
		return  parent;
	}
	public Node getLChild(){
		return lChild;
	}
	public Node getRChild(){
		return rChild;
	}
	public Node getGrandparent(){
		if(parent != null)
			return parent.getParent();
		else
			return null;
	}
	public Node getUncle(){
		Node g = getGrandparent();
		if(g == null)
			return null;
		else{
			if(parent.equals(g.getLChild()))
				return g.getRChild();
			else
				return g.getLChild();
		}
	}
	
	/*
	 * Basic method for comparing two nodes according to their keys.
	 * DEFINE: If current key is larger than n's key, return 1;
	 *         If current key is equal to n's key, return 0;
	 *         If current key is less than n's key, return -1.
	 */
	public abstract int compareTo(Node n);
}


